<template>
  <div id="app" style="height: 100%;">
    <router-view></router-view>
  </div>
</template>

<script>
  import {getMenuRoute} from '@/api/sys/menuApi'
  export default {
    name: 'App',
    data () {
      return {
        menuList:[]
      }
    },
    created () {
      let _this = this;
      if(!this.$router.options.isAddDynamicMenuRoutes){
        getMenuRoute({}).then(res => {
          if(res.code === 200){
            _this.fnAddDynamicMenuRoutes(res.data);
          }
        }).catch(function(error){})
      }
    },
    methods: {
      //添加动态路由
      fnAddDynamicMenuRoutes: function (menuList) {
        for (let i = 0; i < menuList.length; i++) {
          let route = {
            path: menuList[i].route,
            component: () => import(`@/view/Main`),
            name: menuList[i].route.replace('/', ''),
            meta: {
              title: menuList[i].name,
              breadcrumb: true
            }
          };
          //存在子路由
          if (menuList[i].children && menuList[i].children.length >= 1) {
            let childrenRouteList = [];
            let childrenList = menuList[i].children;
            for (let j = 0; j < childrenList.length; j++) {
              let component = childrenList[j].route;
              let path = childrenList[j].route.split('/');
              childrenRouteList.push({
                path: path[path.length-1],
                name: path[path.length-1],
                component: () => import(`@/view${component}`),
                meta: {
                  title: childrenList[j].name,
                  breadcrumb: true,
                }
              })
            }
            route.children = childrenRouteList
          }
          this.$router.options.routes.push(route)
        }
        //添加路由并设置404页面
        this.$router.addRoutes(this.$router.options.routes.concat([{
          path:'*',
          redirect:'/404'
        }]));
        this.$router.options.isAddDynamicMenuRoutes = true;
      }
    }
  }
</script>
